#include "SDL.h"

// Prototypes
void delay(Uint32 delayTime);
Uint32 initDelay();


Uint32 delayResolution = 10;
	
void delay(Uint32 delayTime)
{
		Uint32 startTime = SDL_GetTicks();
		Sint32 burnTime = delayTime;
		Uint32 safeDelayTime;
		
		// Delay for a while
		if(delayTime >= delayResolution)
		{
			safeDelayTime = delayTime - (delayTime % delayResolution);
			SDL_Delay(safeDelayTime);
			burnTime = Sint32(delayTime - (SDL_GetTicks() - startTime));
		}
		
		// Burn the rest in a loop
		while(burnTime > 0)
		{
			burnTime = delayTime - (SDL_GetTicks() - startTime);	
		}
}

Uint32 initDelay()
{
  SDL_Delay(10);  // Give the processor some rest
  delayResolution = SDL_GetTicks();  // Start counting the time
  SDL_Delay(1);  // Delay for a minimal time
  delayResolution = SDL_GetTicks() - delayResolution;  // Save how long that took

  return delayResolution;
}